package com.example.zxw.ui.news.newstabpage

import androidx.lifecycle.viewModelScope
import com.example.zxw.datalayer.NewsRepo
import com.example.zxw.entity.NewsListEntity
import com.example.zxw.net.ApiResult
import com.example.zxw.net.BaseViewModel
import com.example.zxw.ui.views.RequestDataStatus
import com.example.zxw.ui.views.RequestResult
import com.fmt.mvi.learn.gobal.log
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch


data class CommonNewsTabPageData(
    val datas: List<NewsListEntity>? = null,
    val status: RequestDataStatus
)

class CommentNewsTabPageViewModel : BaseViewModel() {

    init {
        "CommentNewsTabPageViewModel init ${this.hashCode()}".log()
    }

    private val _uiState =
        MutableStateFlow(CommonNewsTabPageData(status = RequestDataStatus.LoadMoreIng))
    val uiState = _uiState.asStateFlow()
    private val newsRepo by lazy { NewsRepo() }
    var pageIndex = 1

    /**
     * 获取新闻列表数据
     * 规则：
     * 1. 先从缓存取，然后返回缓存数据
     * 2. 再从服务器取，然后在返回服务器数据
     */
    fun loadData(cname: String) {
        "获取数据".log()
        pageIndex = 1
        viewModelScope.launch {
            _uiState.update {
                it.copy(status = RequestDataStatus.RefreshIng)
            }
            val newsListLocal = newsRepo.getNewsListLocal(cname, pageIndex)
            if (newsListLocal != null) {
                "获取缓存数据成功".log()
                _uiState.update {
                    it.copy(status = RequestDataStatus.RefreshComplete, datas = newsListLocal)
                }
            }

            newsRepo.getNewsListNet(cname, pageIndex).dealResult().also { result ->
                when (result) {
                    is RequestResult.Success -> {
                        "获取网络数据成功".log()
                        pageIndex++
                        _uiState.update {
                            it.copy(status = RequestDataStatus.RefreshComplete, datas = result.data)
                        }
                    }

                    is RequestResult.Fail -> {
                        _uiState.update {
                            it.copy(status = RequestDataStatus.RefreshFail)
                        }
                    }

                    is RequestResult.NoData -> {
                        _uiState.update {
                            it.copy(status = RequestDataStatus.NoData)
                        }
                    }
                }
            }


        }


    }


    fun loadMoreData(cname: String) {
        viewModelScope.launch {
            _uiState.update {
                it.copy(status = RequestDataStatus.LoadMoreIng)
            }
            newsRepo.getNewsListNet(cname, pageIndex).dealResult().also { result ->
                when (result) {
                    is RequestResult.Success -> {
                        "加载数据成功".log()
                        pageIndex++
                        _uiState.update {
                            it.datas.let { it as ArrayList }.addAll(result.data)
                            it.copy(status = RequestDataStatus.LoadMoreComplete, datas = it.datas)
                        }
                    }

                    is RequestResult.Fail -> {
                        "加载数据失败".log()
                        _uiState.update {
                            it.copy(status = RequestDataStatus.LoadMoreFail)
                        }
                    }

                    is RequestResult.NoData -> {
                        "无更多数据".log()
                        _uiState.update {
                            it.copy(status = RequestDataStatus.NoMoreData)
                        }
                    }
                }

            }

        }

    }
}